// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core;

import org.uncommons.maths.random.CellularAutomatonRNG;
import org.uncommons.maths.random.SecureRandomSeedGenerator;
import org.uncommons.maths.random.SeedException;
import org.uncommons.maths.random.SeedGenerator;
import zombie.network.GameServer;

public final class Rand {
    public static CellularAutomatonRNG rand;
    public static CellularAutomatonRNG randlua;
    public static int id = 0;

    public static void init() {
        try {
            rand = new CellularAutomatonRNG(new Rand.PZSeedGenerator());
            randlua = new CellularAutomatonRNG(new Rand.PZSeedGenerator());
        } catch (SeedException seedException) {
            seedException.printStackTrace();
        }
    }

    private static int Next(int _int, CellularAutomatonRNG cellularAutomatonRNG) {
        if (_int <= 0) {
            return 0;
        } else {
            id++;
            if (id >= 10000) {
                id = 0;
            }

            return cellularAutomatonRNG.nextInt(_int);
        }
    }

    public static int Next(int max) {
        return Next(max, rand);
    }

    public static long Next(long _long, CellularAutomatonRNG cellularAutomatonRNG) {
        return (long)Next((int)_long, cellularAutomatonRNG);
    }

    public static long Next(long max) {
        return Next(max, rand);
    }

    public static int Next(int int1, int int0, CellularAutomatonRNG cellularAutomatonRNG) {
        if (int0 == int1) {
            return int1;
        } else {
            if (int1 > int0) {
                int int2 = int1;
                int1 = int0;
                int0 = int2;
            }

            id++;
            if (id >= 10000) {
                id = 0;
            }

            int int3 = cellularAutomatonRNG.nextInt(int0 - int1);
            return int3 + int1;
        }
    }

    public static int Next(int min, int max) {
        return Next(min, max, rand);
    }

    public static long Next(long long1, long long0, CellularAutomatonRNG cellularAutomatonRNG) {
        if (long0 == long1) {
            return long1;
        } else {
            if (long1 > long0) {
                long long2 = long1;
                long1 = long0;
                long0 = long2;
            }

            id++;
            if (id >= 10000) {
                id = 0;
            }

            int _int = cellularAutomatonRNG.nextInt((int)(long0 - long1));
            return (long)_int + long1;
        }
    }

    public static long Next(long min, long max) {
        return Next(min, max, rand);
    }

    public static float Next(float float1, float float0, CellularAutomatonRNG cellularAutomatonRNG) {
        if (float0 == float1) {
            return float1;
        } else {
            if (float1 > float0) {
                float float2 = float1;
                float1 = float0;
                float0 = float2;
            }

            id++;
            if (id >= 10000) {
                id = 0;
            }

            return float1 + cellularAutomatonRNG.nextFloat() * (float0 - float1);
        }
    }

    public static float Next(float min, float max) {
        return Next(min, max, rand);
    }

    public static boolean NextBool(int invProbability) {
        return Next(invProbability) == 0;
    }

    public static int AdjustForFramerate(int chance) {
        if (GameServer.bServer) {
            chance = (int)((float)chance * 0.33333334F);
        } else {
            chance = (int)((float)chance * ((float)PerformanceSettings.getLockFPS() / 30.0F));
        }

        return chance;
    }

    public static final class PZSeedGenerator implements SeedGenerator {
        private static final SeedGenerator[] GENERATORS = new SeedGenerator[]{new SecureRandomSeedGenerator()};

        private PZSeedGenerator() {
        }

        @Override
        public byte[] generateSeed(int _int) {
            for (SeedGenerator seedGenerator : GENERATORS) {
                try {
                    return seedGenerator.generateSeed(_int);
                } catch (SeedException seedException) {
                }
            }

            throw new IllegalStateException("All available seed generation strategies failed.");
        }
    }
}
